package com.wanter.vote.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Random;

import com.wanter.vote.entity.BeVoted;
import com.wanter.vote.entity.Vote;
import com.wanter.vote.mapper.BeVotedMapper;
import com.wanter.vote.service.BeVotedService;
import com.wanter.vote.service.VoteService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 实现类
 * Created by er on 2017/5/17.
 */
@Service
public class BeVotedServiceImpl implements BeVotedService {
    private final Logger logger = Logger.getLogger(this.getClass());
    @Resource
    private BeVotedMapper beVotedMapper;
    @Resource
    private VoteService voteService;

    @Override
    public void insertBeVoted(BeVoted beVoted) {
        beVotedMapper.insert(beVoted);
        if (logger.isInfoEnabled()) {
            logger.info("新增成功" + beVoted);
        }
    }

    @Override
    public void updateBeVoteNum(int id, Vote vote) {
        if (id > 0) {
            int flag = beVotedMapper.updateBeVoteNum(id);
            voteService.insertVote(vote);
            if (flag > 0) {
                if (logger.isDebugEnabled()) {
                    logger.info("id=" + id + "得了一票");
                }
            }
        }
    }

    @Override
    public BeVoted selbyRandom() {
        PageHelper.startPage(1, 1);
        List<BeVoted> list = beVotedMapper.selectAll();
        PageInfo<BeVoted> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Random random = new Random();
        int nextInt = random.nextInt((int) total);
        BeVoted beVoted = null;
        while (beVoted == null) {
            beVoted = beVotedMapper.selectByPrimaryKey(nextInt);
        }
        if (logger.isInfoEnabled()) {
            logger.info("查询成功" + beVoted);
        }
        return beVoted;
    }

    @Override
    public BeVoted selbyPrimaryKey(int id) {
        BeVoted beVoted = beVotedMapper.selectByPrimaryKey(id);
        if (logger.isInfoEnabled()) {
            logger.info("根据id选择" + beVoted);
        }
        return beVoted;
    }

    @Override
    public void updateBeVotePeos(int id, Vote vote) {
        if (id > 0 && !StringUtils.isEmpty(vote.getVoteuserid())) {
            beVotedMapper.updateBeVotePeos(id);
            voteService.insertVote(vote);
            if (logger.isInfoEnabled()) {
                logger.info("id+" + id + "投了反对票");
            }
        }
    }

    @Override
    public void deleteBeVoted(int id) {
        if (id > 0) {
            beVotedMapper.deleteByPrimaryKey(id);
        }
        if (logger.isInfoEnabled()) {
            logger.info("删除被投记录,id为" + id);
        }
    }

    @Override
    public void updateBeVoted(BeVoted beVoted) {
        if (beVoted != null) {
            beVotedMapper.updateByPrimaryKey(beVoted);
            if (logger.isInfoEnabled()) {
                logger.info("更新成功,更新的对象为" + beVoted);
            }
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("更新失败,要更新的对象为空");
            }
        }
    }
}
